package cn.kgc.chlm.servlet;

import cn.kgc.chlm.entity.User;
import cn.kgc.chlm.service.UserService;
import cn.kgc.chlm.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet{

    public void login(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(request,response);
    }

    /**
     * 登录方法
     * @param req
     * @param resp
     */
    public void do_login(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        UserService userService = new UserServiceImpl();

        String username = req.getParameter("username");
        String password = req.getParameter("password");

        User loginUser = userService.login(new User(username,password));
//        System.out.println(loginUser);
        HttpSession session = req.getSession();
        if(null != loginUser){
            session.setAttribute("user",loginUser);
            resp.sendRedirect(req.getContextPath()+"/home");
        }else {
            req.setAttribute("msg","用户名或密码错");
            req.getRequestDispatcher("/WEB-INF/pages/login.jsp").forward(req,resp);
        }
    }

    /**
     * 注册方法
     * @param request
     * @param response
     *
     */
    public void regUser(HttpServletRequest request, HttpServletResponse response){
        System.out.println("用户注册");
    }
}
